package com.tanhua.management.controller;

import com.tanhua.management.service.MessagesService;
import com.tanhua.management.vo.MessagesVo;
import com.tanhua.management.vo.PageResult;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/manage")
public class MessagesController {
    @Autowired
    private MessagesService messagesService;

    /**
     * 查询动态列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @param sortProp
     * @param sortOrder
     * @return
     */
    @GetMapping("/messages")
    public ResponseEntity<PageResult> queryMessagesList(
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize,
            String sortProp, String sortOrder,
            @RequestParam(name = "id", required = false) String id,
            @RequestParam(name = "uid", required = false) String userId,
            Long sd, Long ed, String state
    ) {
        try {

            PageResult pageResult = this.messagesService.queryMessagesList(
                    page, pageSize, sortProp, sortOrder, id, userId, sd, ed, state);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询单个信息表
     *
     * @param publishId
     * @return
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity<MessagesVo> queryMessagesById(@PathVariable("id") String publishId) {
        try {
            MessagesVo messagesVo = this.messagesService.queryMessagesById(publishId);
            if (null != messagesVo) {
                return ResponseEntity.ok(messagesVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询一个动态的评论
     *
     * @param publishId
     * @param page
     * @param pageSize
     * @param sortProp
     * @param sortOrder
     * @return
     */
    @GetMapping("/messages/comments")
    public ResponseEntity<PageResult> queryCommentsList(@RequestParam(value = "messageID") String publishId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                        String sortProp, String sortOrder) {

        try {
            PageResult pageResult = this.messagesService.queryCommentsList(publishId, page, pageSize, sortProp, sortOrder);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

   /* *//**
     * 更改置顶
     *
     * @param messagesId
     * @param top
     * @return
     *//*
    @PostMapping("{id}/{topState}")
    public ResponseEntity<String> updateTopStatusById(@PathVariable("id") String messagesId,
                                                      @PathVariable("top") String top) {
        String message = null;
        try {
            message = this.messagesService.updateTopStateById(messagesId, top);
            if (null != message) {
                //操作成功
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(message);
    }*/

    /**
     * 审核通过  /manage/messages/pass
     *
     * @param publishIds
     * @return
     */
    @PostMapping("/messages/pass")
    public ResponseEntity<String> passMessages(@RequestBody ArrayList<String> publishIds) {
        //审核状态，1为待审核，2为自动审核通过，3为待人工审核，4为人工审核拒绝，5为人工审核通过，6为自动审核拒绝

        try {
            Long count = this.messagesService.passMessages(publishIds);
            if (publishIds.size()==count) {
                //操作成功
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }
    /**
     * 审核不通过
     *
     * @param publishIds
     * @return
     */
    @PostMapping("/messages/reject")
    public ResponseEntity<String> rejectMessages(@RequestBody ArrayList<String> publishIds) {
        //审核状态，1为待审核，2为自动审核通过，3为待人工审核，4为人工审核拒绝，5为人工审核通过，6为自动审核拒绝

        try {
            Long count = this.messagesService.rejectMessages(publishIds);
            if (publishIds.size()==count) {
                //操作成功
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

    /**
     *审核撤销
     *把审核状态改为待审核状态
     * @param publishIds
     * @return
     */
    @PostMapping("/messages/revocation")
    public ResponseEntity<String> revocationMessages(@RequestBody ArrayList<String> publishIds) {
        //审核状态，1为待审核，2为自动审核通过，3为待人工审核，4为人工审核拒绝，5为人工审核通过，6为自动审核拒绝

        try {
            Long count = this.messagesService.revocationMessages(publishIds);
            if (publishIds.size()==count) {
                //操作成功
                return ResponseEntity.ok("操作成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("操作失败");
    }

}
